package cn.com.open.errorquestion.service.impl;

import cn.com.open.errorquestion.mapper.ErrorQuestionMapper;
import cn.com.open.errorquestion.model.AnswerQuestionLog;
import cn.com.open.errorquestion.model.MGQuestionLog;
import cn.com.open.errorquestion.service.ErrorQuestionService;
import cn.com.open.errorquestion.vo.ErrorQuestionVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ErrorQuestionServiceImpl implements ErrorQuestionService {

    @Autowired
    private ErrorQuestionMapper errorQuestionMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存错题
     * @param answerData
     */
    @Override
    public void save(AnswerQuestionLog answerData) {

        errorQuestionMapper.save(answerData);
    }

    /**
     * 根据试题ID  删除错题
     * @param answerData
     */
    @Override
    public void delete(AnswerQuestionLog answerData) {
        errorQuestionMapper.delete(answerData);
    }

    @Override
    public List<ErrorQuestionVo> list() {


        return errorQuestionMapper.list();
    }

    @Override
    public List<MGQuestionLog> list(Integer pageNum, Integer pageSize) {
        Query query = new Query();

        //多条件查询
//        Criteria criteria = new Criteria();
////        criteria.andOperator(
////                Criteria.where("id").is(""),
////                Criteria.where("").is(""),
////                Criteria.where("").is(""));
////        query.addCriteria(criteria);

        //排序
//        List<Sort.Order> orders = new ArrayList<>();
//        orders.add(new Sort.Order(Sort.Direction.DESC,"id"));
//        query.with(new Sort(orders));

        //分页
        query.skip(pageSize * (pageNum - 1)).limit(pageSize);

        //查询
        List<MGQuestionLog> mgQuestionLogList = mongoTemplate.find(query, MGQuestionLog.class);
        return mgQuestionLogList;
    }
}
